package org.ajc.gift.utils;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import org.json.JSONException;
import org.json.JSONObject;

public class BaiduAiUtils {

    //设置APPID/AK/SK
    public static final String APP_ID = "52702303";
    public static final String API_KEY = "J899cv5yTLoellPlOxoorf58";
    public static final String SECRET_KEY = "LDOb1cUu110UG0uSh0YR1vE850GXrteX";

    private static AipContentCensor client;

    static {
        // 初始化一个AipContentCensor
        client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    }

    /**
     * @param text 要审核文本
     * @return 审核结果  false代表不合规  true 合规
     */
    public static boolean textReview(String text) {

        try {
            JSONObject response = client.textCensorUserDefined(text);
            int conclusionType = response.getInt("conclusionType");
            return conclusionType == 1 ? true : false;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @param url 要审核图像网络地址
     * @return 审核结果  false代表不合规  true 合规
     */
    public static boolean imgReview(String url) {

        try {
            // 参数为url
            JSONObject response = client.imageCensorUserDefined(url, EImgType.URL, null);
            int conclusionType = response.getInt("conclusionType");
            System.out.println(response.toString());
            return conclusionType == 1 ? true : false;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(textReview("成都市武侯区孵化园-地铁站"));
//        System.out.println(imgReview());
    }
}